package webx.actions;

import java.util.ArrayList;

import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.Path;
import org.eclipse.search.ui.ISearchQuery;
import org.eclipse.search.ui.NewSearchUI;
import org.eclipse.search.ui.text.TextSearchQueryProvider;

public class SearchFileRunnable implements Runnable {
	
	String scope = "";
	
	String string = "";
	
	public SearchFileRunnable() {
	}
	
	public SearchFileRunnable(String scope , String string ) {
		this.scope = scope;
		this.string = string;
	}
	
	public static void main(String[]args){
		System.out.println("123_".split("_",0)[0]);
	}
	
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public void run() {
    	try{
    	   TextSearchQueryProvider provider = TextSearchQueryProvider.getPreferred();
    	   
    	   System.out.println("provider:");
			try
			{   
				 //IWorkspace resource = ResourcesPlugin.getWorkspace();
					//IProject project = resource.getRoot().getProject(scope);
					ArrayList list = new ArrayList();
					String[] scopes = scope.split(";");
					for(int i=0;i<scopes.length;i++){
						IResource iresource = ResourcesPlugin.getWorkspace().getRoot().findMember(new Path(scopes[i]));
						if(iresource!=null && iresource.exists()){
							list.add(iresource);
						}
					}
					//FileTextSearchScope.newSearchScope((IResource[])list.toArray(new IResource[0]), new String[]{"txt","html"}, false);
					//if (project != null)
					ISearchQuery query = provider.createQuery(string,(IResource[])list.toArray(new IResource[0]));
					if (query != null)
					NewSearchUI.runQueryInBackground(query);
			}catch (Throwable e){  
				e.printStackTrace();
			}
		    // searching end
    	}catch(Exception ex){
    		ex.printStackTrace();
    	}
	}
}
